// 通知类消息格式化处理
export function setNotificationMsg ({ info, isGroup, chatRoomInfo, friendList, roomId }) {
  const { subType, members, roomId: cardRoomId, userIds, userName } = info
  let operInfo = info.operInfo
  if (!operInfo && isGroup) {
    const obj = chatRoomInfo[cardRoomId].data.find(i => i.adminType === 9)
    if (obj) { operInfo = obj.name }
  }
  let msg
  switch (subType) {
    case 'recall':
      if (isGroup) { msg = `<span style="color: #508AFF">“${getName(info, isGroup, chatRoomInfo, roomId, friendList)}”</span>撤回了一条消息` } else { msg = '对方撤回' }
      break
    case 'withdraw':
      msg = `你撤回了一条消息`; break
    case 'bePulledBlack':
      msg = `消息已发出，但被对方拒收了`; break
    case 'create':
      msg = `${namePronoun(operInfo.userId, operInfo.name)}邀请${getNameByArr(userIds, chatRoomInfo, roomId)}加入了群聊`; break
    case 'addUser':
      msg = `${getNameByArr(userIds, chatRoomInfo, roomId)}加入了群聊`; break
    case 'dissolved':
      msg = `该群已解散`; break
    case 'exitGroup':
      msg = `${userName}退出群聊`; break
    case 'delGroupMember':
      msg = `${getNameByArr(userIds, chatRoomInfo, roomId)}被移出群聊`; break
    case 'addAdmin':
      msg = `${getNameByObjArr(members)}被设置为管理员`; break
    case 'removeAdmin':
      msg = `${getNameByObjArr(members)}从管理员移除`; break
    case 'GROUP_BANNED':
      msg = `该群已经被封禁`; break
    case 'openGroupForbidSpeak':
      msg = `${areYouGroupLeader(chatRoomInfo[cardRoomId])}开启了全员禁言`; break
    case 'closeGroupForbidSpeak':
      msg = `${areYouGroupLeader(chatRoomInfo[cardRoomId])}关闭了全员禁言`; break
    case 'openUserForbidSpeak':
      msg = `${namePronoun(members[0].userId, members[0].name)}已被禁言`; break
    case 'closeUserForbidSpeak':
      msg = `${namePronoun(members[0].userId, members[0].name)}已被解除禁言`; break
  }
  return msg
}

export function getNameByObjArr (arr) {
  const str = arr.reduce((accumu, current) => `${accumu}${namePronoun(current.userId, current.name)},`, '')
  return str.slice(0, -1)
}

export function getNameByArr (arr, chatRoomInfo, roomId) {
  const res = chatRoomInfo[roomId].data.filter(i => arr.includes(i.userId + ''))
  const str = res.reduce((accumu, current) => `${accumu}${namePronoun(current.userId, current.name)},`, '')
  return str.slice(0, -1)
}

export function namePronoun (id, name) {
  const myId = sessionStorage.getItem('aillo_id')
  return myId === id + '' ? '你' : `“${name}”`
}

export function areYouGroupLeader (roomMenber) {
  const obj = roomMenber.data.find(i => i.adminType === 9)
  const myId = sessionStorage.getItem('aillo_id')
  let pronoun = '群主'
  if (obj) {
    myId === obj.userId + '' && (pronoun = '你')
  }
  return pronoun
}

export function getName (item, isGroup, chatRoomInfo, roomId, friendList) {
  let who = ''
  if (isGroup) {
    const groupMembers = chatRoomInfo[roomId].data
    who = groupMembers.find(e => e.userId + '' === item.sender + '' || e.userId + '' === item.senderId + '')
    if (who) { return who.name || who.nickName }
  } else {
    who = friendList.find(e => e.roomId + '' === roomId)
    if (who) { return who.name || who.nickName }
  }
  return null
}
